package com.rem.cloudalibaba.service.impl;

import com.rem.cloudalibaba.entity.CommonResult;
import com.rem.cloudalibaba.entity.Order;
import com.rem.cloudalibaba.entity.Product;
import com.rem.cloudalibaba.mapper.OrderMapper;
import com.rem.cloudalibaba.service.OrderService;
import com.rem.cloudalibaba.service.ProductService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Rem
 * @date 2021/7/13
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderMapper orderMapper;


    @Autowired
    ProductService productService;

    /**
     * 根据商品创建订单
     *
     * @param product
     */
    @Override
    public void createOrder(Product product) {

        Order order = new Order();
        order.setPid(product.getPid()).setPname(product.getPname()).setPprice(product.getPprice()).setNumber(1);
        orderMapper.insert(order);
    }


    /**
     * 创建订单
     *
     * @param pid 商品id
     * @param num 购买量
     */
    @Override
    @GlobalTransactional
    public void createOrder(Integer pid, Integer num) throws RuntimeException {
        try {
            CommonResult<Product> result = productService.redStock(pid, num);
            Product data = result.getData();
            Order order = new Order();
            order.setPid(data.getPid()).setPname(data.getPname()).setPprice(data.getPprice()).setNumber(1);
            int a = 1 / 0;
            orderMapper.insert(order);
        } catch (Exception e) {
            throw new RuntimeException("创建异常");
        }
    }


}
